package com.pp.admin.main.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.pp.admin.common.client.AdminLocale;
import com.pp.admin.common.client.PermissionCheck;
import com.pp.admin.common.client.beans.LoginUserInfo;
import com.pp.admin.common.client.event.AfterLogoutHandler;
import com.pp.admin.common.client.helper.JavaScriptCommon;
import com.pp.admin.common.client.i18.I18;
import com.pp.admin.common.client.service.Service;

public class HeaderPanel extends HorizontalPanel implements I18, Service {

	private Administrator parent = null;

	private Anchor signin = new Anchor(myConstants.login());

	private Anchor signOut = new Anchor(myConstants.logout());

	private AdminLocale localeBox = null;

	private Label welcomeMsg = null;

	private List<AfterLogoutHandler> listHandlers = new ArrayList<AfterLogoutHandler>();

	private String loginPath = "Login.html";

	public HeaderPanel(Administrator parent) {

		this.parent = parent;
		localeBox = new AdminLocale(this);
		welcomeMsg = new Label(msgs.welcomemessage(Administrator
				.getCurrentUserInfo().toString()));
		welcomeMsg.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		this.add(welcomeMsg);
		this.add(new HTML(" &nbsp;|&nbsp;"));
		this.add(localeBox);
		this.add(new HTML(" &nbsp;|&nbsp;"));
		setSigninPanel();
	}

	public void setSigninPanel() {

		welcomeMsg.setText(msgs.welcomemessage(Administrator
				.getCurrentUserInfo().toString()));
		signin.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		signin.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				JavaScriptCommon.redirectToUrl(loginURL());
			}
		});
		if (this.getWidgetCount() > 4) {
			this.remove(signOut);
		}
		this.add(signin);

	}

	public void setUserInfo(LoginUserInfo user) {

		welcomeMsg.setText(msgs.welcomemessage(user.getUserID()));
		signOut.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		signOut.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				loginService.logOut(new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {

						// TODO Auto-generated method stub
					}

					@Override
					public void onSuccess(Void result) {

						Administrator.setCurrentUserInfo(new LoginUserInfo(
								myConstants.guest(), PermissionCheck.APP_DOMAIN));
						Iterator<AfterLogoutHandler> itrHandlers = listHandlers
								.iterator();
						AfterLogoutHandler afterLogoutHandler = null;
						while (itrHandlers.hasNext()) {
							afterLogoutHandler = itrHandlers.next();
							afterLogoutHandler.afterLogout();
						}
						setSigninPanel();
					}
				});
			}
		});

		this.remove(signin);
		this.add(signOut);
	}

	public void addAfterLogoutHandler(AfterLogoutHandler handler) {

		listHandlers.add(handler);
	}

	public String loginURL() {

		StringBuilder loginURL = new StringBuilder();
		loginURL.append(GWT.getHostPageBaseURL());
		loginURL.append(loginPath);
		loginURL.append(Location.getQueryString());
		return loginURL.toString();
	}
}
